package com.atguigu.gulimall.ware.testCAS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class test {

    public static class Counter{
        //使用原子类，一次修改只能有一个线程，如果有一个线程修改了，那么另外一个线程就会放弃，保证原子性，不会出现线程安全的问题
        //public AtomicInteger count = new AtomicInteger(100);
        //Integer count = 100;
        int count=0;
        public void increase(){
            count++;
        }

        LongAdder incrLAdr = new LongAdder();
        public void incrByLongLAdr(){
            incrLAdr.increment();
        }

    }

    public static void main(String[] args) throws InterruptedException {

        //AtomicInteger integer = new AtomicInteger(100);
        //new Integer(100);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();

        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();


        Counter counter = new Counter();
        CountDownLatch countDownLatch = new CountDownLatch(50);

        long st = System.currentTimeMillis();
        for (int i = 0; i < 50; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j < 100*10000; j++) {
                        counter.incrLAdr.increment();
                    }
                }finally {
                    countDownLatch.countDown();
                }
            }).start();
        }
        countDownLatch.await();
        long et = System.currentTimeMillis();
        System.out.println(counter.incrLAdr.sum()+"----"+(et-st));

    }
}
